<template>

    <div class="todo-footer" v-show="todos.length">
      <label>
        <input type="checkbox" :checked="isALL" @click="checkALL"/>
      </label>
      <span>
          <span>已完成{{doneTotal}}</span> / 全部{{todos.length}}
        </span>
      <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
    </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Footer",
  props:['todos',],
  computed:{
    // eslint-disable-next-line vue/return-in-computed-property
    doneTotal(){
      // reduce 条件统计 pre是每次循环的返回值 current是每一项
      return this.todos.reduce((pre,current)=>{
        return pre + (current.done ? 1 : 0) //完成的个数
      },0)
   },
    isALL(){
      return this.doneTotal === this.todos.length && this.todos.length>0
    }
  },
  //实现全选全不选
  methods:{
    checkALL(e){
      if (e.target.checked){
        this.todos.forEach((todo)=>{
          todo.done = true
        })
      }else {
        this.todos.forEach((todo)=>{
          todo.done = false
        })
      }
    },
    clearAll(){
      if(confirm('确定删除吗')){
        this.$emit('clearAllTodo')
      }
    }
  }


}
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>